<?php

namespace app\mingteng\controller;

use think\AjaxPage;
use think\Db;
use Think\Page;

class Shop extends Base
{
    public $listRows = 20;

    //店铺列表
    public function index(){
        return $this->fetch();
    }

    public function ajax_index(){
        $params = input('params.');
        $page = input('page', '1');

        $where = array();
        $where['shopIsDel'] = array('=', '0');
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $where['shopPhone|shopName'] = array('like', '%'.$params['keywords'].'%');
        }
        if (isset($params['searchAds']) && !empty($params['searchAds'])) {
            $where['shopProvince|shopCity|shopArea|shopAddress'] = array('like', '%'.$params['searchAds'].'%');
        }
        if (isset($params['shopStatus']) && $params['shopStatus'] >= '0') {
            $where['shopStatus'] = array('=', $params['shopStatus']);
        }

        $count = Db::name('shop')->where($where)->count();
        $list = Db::name('shop')->where($where)->page($page, $this->listRows)->select();

        $ajaxPage = new AjaxPage($count, $this->listRows);
        $show = $ajaxPage->show();

        $this->assign('list', $list);
        $this->assign('page', $show);
        return $this->fetch();
    }

    public function add(){
        return $this->fetch();
    }

    public function doAdd(){
        $params = input('post.');

        foreach ($params as &$value){
            if (empty($value)) {
                return json(array("code" => 500, "msg" => "填写信息不能为空"));
            }
        }

        $address = $params['shopProvince'] . $params['shopCity'] . $params['shopArea'] . $params['shopAddress'];
        $geocoder = geocoder($address);
        if ($geocoder['status'] != '0') {
            return json(array('code' => 500, 'msg' => '获取地址坐标出错,请核对地址信息'));
        }

        $catch = array(
            'shopName' => $params['shopName'], //店铺名称
            'shopLogo' => config('site_url.site_url') . $params['shopLogo'], //店铺LOGO
            'shopPhone' => $params['shopPhone'], //联系电话
            'shopProvince' => $params['shopProvince'], //省
            'shopCity' => $params['shopCity'], //市
            'shopArea' => $params['shopArea'], //区
            'shopAddress' => $params['shopAddress'], //详细地址
            'shopLat' => $geocoder['result']['location']['lat'], //纬度
            'shopLng' => $geocoder['result']['location']['lng'], //经度
            'shopBusinessTime' => $params['shopBusinessTime'], //营业时间
            'shopStatus' => $params['shopStatus'], //展示状态
        );
        $shopId = Db::name('shop')->insert($catch);
        return json(array(
            'code' => 200,
            'msg' => '添加成功'
        ));
    }

    public function edit(){
        $shopId = input('shopId');

        $shopInfo = Db::name('shop')->where('shopId', '=', $shopId)->find();
        $this->assign('shop', $shopInfo);

        return $this->fetch();
    }

    public function doEdit(){
        $params = input('post.');

        $address = $params['shopProvince'] . $params['shopCity'] . $params['shopArea'] . $params['shopAddress'];
        $geocoder = geocoder($address);
        if ($geocoder['status'] != '0') {
            return json(array('code' => 500, 'msg' => '获取地址坐标出错,请核对地址信息'));
        }

        $params['shopLogo'] = strstr($params['shopLogo'],"public");
        $catch = array(
            'shopName' => $params['shopName'], //店铺名称
            'shopLogo' => config('site_url.site_url') . "/" . $params['shopLogo'], //店铺LOGO
            'shopPhone' => $params['shopPhone'], //联系电话
            'shopProvince' => $params['shopProvince'], //省
            'shopCity' => $params['shopCity'], //市
            'shopArea' => $params['shopArea'], //区
            'shopAddress' => $params['shopAddress'], //详细地址
            'shopLat' => $geocoder['result']['location']['lat'], //纬度
            'shopLng' => $geocoder['result']['location']['lng'], //经度
            'shopBusinessTime' => $params['shopBusinessTime'], //营业时间
            'shopStatus' => $params['shopStatus'], //展示状态
        );
        Db::name('shop')->where('shopId', '=', $params['shopId'])->save($catch);
        return json(array(
            'code' => 200,
            'msg' => '编辑成功',
        ));
    }

    public function del(){
        $params = input('post.');

        Db::name('shop')->where('shopId', '=', $params['del_id'])->save(array(
            'shopIsDel' => '1',
        ));
        return json(array(
            'code' => 200,
            'msg' => '操作成功',
        ));
    }

}